#include "stdafx.h"
#include "ass_json_util.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"

static const char* kEmpty = "";

void AssJsonUtil::AddJsonDictStringValue(rapidjson::Value& dict,
	rapidjson::Document::AllocatorType& allocator,
	const char* key, const char* str){
		if(!str)
			str = kEmpty;
		rapidjson::Value value(rapidjson::kStringType);
		value.SetString(str,allocator);
		dict.AddMember(rapidjson::Value(key,allocator),value,allocator);
}

void AssJsonUtil::AddJsonDictInt64Value(rapidjson::Value& dict,rapidjson::Document::AllocatorType& allocator,
	const char* key,int64_t bb){
		rapidjson::Value value(rapidjson::kNumberType);
		value.SetInt64(bb);
		dict.AddMember(rapidjson::Value(key,allocator),value,allocator);
}

void AssJsonUtil::AddJsonDictObjectValue(rapidjson::Value& dict,rapidjson::Document::AllocatorType& allocator,
		const char* key,rapidjson::Value& value)
{
	dict.AddMember(rapidjson::Value(key,allocator),value,allocator);
}

void AssJsonUtil::AddJsonDictUInt64Value(rapidjson::Value& dict,rapidjson::Document::AllocatorType& allocator,
	const char* key,uint64_t bb){
	rapidjson::Value value(rapidjson::kNumberType);
	value.SetUint64(bb);
	dict.AddMember(rapidjson::Value(key,allocator),value,allocator);
}

void AssJsonUtil::AddJsonDictBooleanValue(rapidjson::Value& dict,rapidjson::Document::AllocatorType& allocator,
		const char* key,bool value)
{
	int flag = (value)?rapidjson::kTrueType:rapidjson::kFalseType;
	dict.AddMember(rapidjson::Value(key,allocator),value,allocator);
}

void AssJsonUtil::AddJsonDictIntValue(rapidjson::Value& dict,rapidjson::Document::AllocatorType& allocator,
	const char* key,int bb){
		rapidjson::Value value(rapidjson::kNumberType);
		value.SetInt(bb);
		dict.AddMember(rapidjson::Value(key,allocator),value,allocator);
}

void AssJsonUtil::PushJsonArrayObjectValue(rapidjson::Value& array,rapidjson::Document::AllocatorType& allocator,
	rapidjson::Value& one)
{
	array.PushBack(one,allocator);
}

void AssJsonUtil::PushJsonArrayIntValue(rapidjson::Value& array,rapidjson::Document::AllocatorType& allocator,
	int value){
		array.PushBack(value,allocator);
}

void AssJsonUtil::PushJsonArrayInt64Value(rapidjson::Value& array,rapidjson::Document::AllocatorType& allocator,
		int64_t value)
{
	array.PushBack(value,allocator);
}

void AssJsonUtil::PushJsonArrayStringValue(rapidjson::Value& array,rapidjson::Document::AllocatorType& allocator,
	const char* str)
{
	rapidjson::Value value(rapidjson::kStringType);
	value.SetString(str,allocator);
	array.PushBack(value,allocator);
}

std::string AssJsonUtil::ToString(rapidjson::Value& value){
	rapidjson::StringBuffer buffer;  
	rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);  
	value.Accept(writer);  
	return buffer.GetString();
}

int64_t AssJsonUtil::JsonObjectToInt(rapidjson::Value& object,const char* key)
{
	if(object.HasMember(key))
		return object[key].GetInt64();
	
	return 0;
}

std::string AssJsonUtil::JsonObjectToString(rapidjson::Value& object,const char* key)
{
	if(object.HasMember(key))
		return object[key].GetString();
	return kEmpty;
}
